package com.letv.android.client.parser;

import android.text.TextUtils;
import com.letv.android.client.constant.LetvPageStyle;
import com.letv.core.bean.HomeBlock;
import com.letv.core.bean.HomeMetaData;
import com.letv.core.bean.HomePageBean;
import com.letv.core.bean.HomePageBean.Booting;
import com.letv.core.bean.RedirectData;
import com.letv.core.bean.SiftKVP;
import com.letv.core.parser.LetvMobileParser;
import com.letv.core.utils.LogInfo;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * 5.3新首页解析器
 * */
public class HomePageBeanParser extends LetvMobileParser<HomePageBean> {

	/**
	 * 接口返回数据节点
	 * */
	protected final String FOCUS = "focus";
	
	protected final String BLOCK = "block";
	
	protected final String BOOTIMG = "bootimg";
	
	@Override
	public HomePageBean parse(JSONObject data) throws Exception {
		// 解析焦点数据
		HomePageBean homePageBean = new HomePageBean();
		HomePageBean.sHasLetvShop = false;
		if (has(data, FOCUS)) {
			JSONArray arr = getJSONArray(data, FOCUS);
			if (arr != null && arr.length() > 0) {
				ArrayList<HomeMetaData> focusList = new ArrayList<HomeMetaData>();
				for (int i = 0; i < arr.length(); i++) {
					JSONObject objs = getJSONObject(arr, i);
					if (objs != null) {
						HomeMetaData focusMetaData = parserMetaData(objs);
                        if (focusMetaData != null) {
                            focusList.add(focusMetaData);
                        }
					}
				}
				
				homePageBean.setFocus(focusList);
			}
		}

		// 解析主体区块数据
		if (has(data, BLOCK)) {
			JSONArray array = getJSONArray(data, BLOCK);
			if (array != null && array.length() > 0) {
				ArrayList<HomeBlock> blocks = new ArrayList<HomeBlock>();
				
				for (int i = 0; i < array.length(); i++) {
					JSONObject object = getJSONObject(array, i);
					
					if (object != null) {
						HomeBlock block = new HomeBlock();
						block.setBlockname(getString(object, "blockname"));
						block.setCid(getString(object, "cid"));
						JSONArray listArray = getJSONArray(object, "list");

                        if (has(object, "area")) {
                            block.setArea(getString(object, "area"));
                        }

                        if (has(object, "bucket")) {
                            block.setBucket(getString(object, "bucket"));
                        }

                        if (has(object, "cms_num")) {
                            block.setCms_num(getString(object, "cms_num"));
                        }

                        if (has(object, "reid")) {
                            block.setReid(getString(object, "reid"));
                        }

                        //(version >= 5.5)新增字段
                        if (has(object, "contentStyle")) {
                        	LogInfo.log("", "contentStyle :>>" + getString(object, "contentStyle"));
                        	if (LetvPageStyle.SHOP.equals(getString(object, "contentStyle"))){
                        		HomePageBean.sHasLetvShop = true;
                        	}
                            block.setContentStyle(getString(object, "contentStyle"));
                        }
                        /**
                         * version=5.5.1新增字段
                         */
                        if (has(object, "fragId")) {
                        	block.setFragId(getString(object, "fragId"));
                        }

                        //(version >= 5.5)新增跳转字段
                        block.setRedirectData(parserRedirectData(object));

						if (listArray != null && listArray.length() > 0) {
                            if (LetvPageStyle.LETV_SEARCH_HOT.equals(block.getContentStyle())) {
                                ArrayList<String> searchwords = new ArrayList<String>();
                                for (int hotIndex = 0; hotIndex < listArray.length(); hotIndex++) {
                                    JSONObject hotObject = getJSONObject(listArray, hotIndex);
                                    if (hotObject != null) {
                                        searchwords.add(getString(hotObject, "nameCn"));
                                    }
                                }

                                homePageBean.setSearchWords(searchwords);
                            } else if (LetvPageStyle.RECOMMEND.equals(block.getContentStyle())) {
                                ArrayList<HomeMetaData> recommendList = new ArrayList<HomeMetaData>();

                                for (int j = 0; j < listArray.length(); j++) {
                                    HomeMetaData metaData = parserMetaData(getJSONObject(listArray, j));
                                    if (metaData != null) {
                                        recommendList.add(metaData);
                                    }
                                }

                                block.setList(recommendList);
                                homePageBean.setRecommend(block);
                                continue;
                            } else if (LetvPageStyle.LIVE.equals(block.getContentStyle())) {
                                //do nothing
                                blocks.add(block);
                                continue;
                            } else {
                                ArrayList<HomeMetaData> list = new ArrayList<HomeMetaData>();

                                for (int j = 0; j < listArray.length(); j++) {
                                    HomeMetaData metaData = parserMetaData(getJSONObject(listArray, j));
                                    if (metaData != null) {
                                        list.add(metaData);
                                    }
                                }
                                
                                if (LetvPageStyle.SHOP.equals(block.getContentStyle()) && list.size() != 0){
                                	HomeMetaData homeMetaData = list.get(0);
                                	if (homeMetaData != null){
                                		String isResc = homeMetaData.getIs_rec();
                                		LogInfo.log("", " homepage parse shop isResc : " + isResc);
                                		if ("true".equals(isResc)){
                                			HomePageBean.sHasLetvShop = false;
                                			block = null;
                                		}
                                	}
                                }
                                if (block != null){
                                	block.setList(list);
                                }
                            }
                        }
						LogInfo.log("", " homepage parse block : " + block);
						if (block != null){
							block.toHomeChildItemData();
							blocks.add(block);
						}
					}
				}
				homePageBean.setBlock(blocks);
			}
		}

		// 开机大图
		if (has(data, BOOTIMG)) {
			JSONArray bootArray = getJSONArray(data, BOOTIMG);
			
			if (bootArray != null && bootArray.length() > 0) {
				ArrayList<Booting> bootings = new ArrayList<HomePageBean.Booting>();
				
				for (int i = 0, index=0; i < bootArray.length(); i++) {
					Booting booting = new Booting();
					JSONObject object = getJSONObject(bootArray, i);
					
					if (object != null) {
						booting.setName(getString(object, "name"));
						booting.setPic(getString(object, "pic_1"));
						booting.setPushpic_endtime(getString(object, "pushpic_endtime"));
						booting.setPushpic_starttime(getString(object, "pushpic_starttime"));
						//booting.setOrder(getInt(object, "order"));
						//start modified by zengsonghai 20140113 解决order 顺序不对 图片显示不出来问题
						index ++;
						booting.setOrder(index);
						//end modified by zengsonghai 20140113
						bootings.add(booting);
					}
				}
				
				homePageBean.setBootings(bootings);
			}
		}

		return homePageBean;
	}

	protected HomeMetaData parserMetaData(JSONObject objs) throws JSONException {
		HomeMetaData metadata = new HomeMetaData();

        int at = getInt(objs, "at");
        if (!metadata.gotoList.contains(at)) {
            return null;
        }
        
        metadata.setShorDesc(getString(objs, "shorDesc"));
        metadata.setAt(at);
        metadata.setCmsid(getString(objs, "cmsid"));
		metadata.setPid(getInt(objs, "pid"));
		metadata.setVid(getInt(objs, "vid"));
		metadata.setZid(getString(objs, "zid"));
		metadata.setNameCn(getString(objs, "nameCn"));
		metadata.setSubTitle(getString(objs, "subTitle"));
		metadata.setCid(getInt(objs, "cid"));
		metadata.setType(getInt(objs, "type"));
        metadata.setAlbumType(getString(objs, "albumType"));
        metadata.setVideoType(getString(objs, "videoType"));
        metadata.setVarietyShow(getInt(objs, "varietyShow") == 1);

		metadata.setEpisode(getString(objs, "episode"));
		metadata.setNowEpisodes(getString(objs, "nowEpisodes"));
		metadata.setIsEnd(getInt(objs, "isEnd"));
		metadata.setPay(getInt(objs, "pay"));
		metadata.setTag(getString(objs, "ledimVedioTagBean"));
		metadata.setStreamCode(getString(objs, "streamCode"));
		metadata.setWebUrl(getString(objs, "webUrl"));
		metadata.setWebViewUrl(getString(objs, "webViewUrl"));
		metadata.setStreamUrl(getString(objs, "streamUrl"));
		metadata.setTm(getString(objs, "tm"));
		metadata.setDuration(getString(objs, "duration"));
		metadata.setMobilePic(getString(objs, "mobilePic"));
		metadata.setIs_rec(getString(objs, "is_rec"));
        metadata.setPageid(getString(objs, "pageid"));

        metadata.setLiveid(getString(objs, "liveid"));
        metadata.setHomeImgUrl(getString(objs, "homeImgUrl"));
        metadata.setGuestImgUrl(getString(objs, "guestImgUrl"));
        metadata.setId(getString(objs, "id"));
        
		if (has(objs, "singer")) {
			metadata.setSinger(getString(objs, "singer"));
		}
		
		if (has(objs, "showTagList")) {
			JSONArray jo = getJSONArray(objs, "showTagList");
			if (jo != null && jo.length() > 0) {
				ArrayList<SiftKVP> siftKVPs = new ArrayList<SiftKVP>();
				for (int i = 0; i < jo.length(); i++) {
					JSONObject job = getJSONObject(jo, i);
					if (job != null) {
						SiftKVP showTagList = new SiftKVP();

						showTagList.setId(getString(job, "id"));
						showTagList.setKey(getString(job, "value"));
						showTagList.setFilterKey(getString(job, "key"));

						siftKVPs.add(showTagList);
					}
				}

				metadata.setShowTagList(siftKVPs);
			}
		}

		return metadata;
	}

    protected RedirectData parserRedirectData(JSONObject objs) throws JSONException {
        RedirectData redirectData = new RedirectData();

        redirectData.setRedirectCid(getString(objs, "redirectCid"));
        redirectData.setRedirectPageId(getString(objs, "redirectPageId"));
        redirectData.setRedirectType(getInt(objs, "redirectType"));
        redirectData.setRedirectUrl(getString(objs, "redirectUrl"));
        redirectData.setRedirectVideoType(getString(objs, "redirectVideoType"));

        if (has(objs, "redField")) {
            JSONArray array = getJSONArray(objs, "redField");
            if (array != null && array.length() > 0) {
                HashMap<String, String> redirectMap = new HashMap<String, String>();
                for (int i = 0; i < array.length(); i++) {
                    JSONObject job = getJSONObject(array, i);
                    String redFieldType = getString(job, "redFieldTypeList");
                    String redFieldDetail = getString(job, "redFieldDetailList");
                    if (job != null && !TextUtils.isEmpty(redFieldType) && !TextUtils.isEmpty(redFieldDetail)) {
                        redirectMap.put(redFieldType, redFieldDetail);
                    }
                }

                redirectData.setRedField(redirectMap);
            }
        }

        return redirectData;
    }
}
